





#include "main.h"


/********************************************************************************************************************
|   Macro Definition
|*******************************************************************************************************************/

/********************************************************************************************************************
|   Enum Definition
|*******************************************************************************************************************/


/********************************************************************************************************************
|   Typedef Definition
|*******************************************************************************************************************/
typedef enum	TaskStatus
{
	TIMER_TASK_IDLE = 0U,
	TIMER_TASK_ENABLE,
	TIMER_TASK_DISABLE,
}e_TaskStatusType;							/*任务状态*/


typedef	struct	TimerScheduler				/*定时任务调度表结构体*/
{
	e_TaskNameType		e_TaskName;			/*任务名*/
	e_TaskStatusType	e_TaskStatus;		/*任务状态*/
	u8	ui8_TaskRunCount;				/*任务运行次数*/
	u32	ui32_TaskTickOffset;			/*任务计时节拍偏移量*/
	u32	ui32_TaskTickTop;				/*任务计时节拍最大值*/
	u32	ui32_TaskTickCount;				/*任务计时节拍计数*/
	u32	ui32_CurrentTaskTimeConsuming;	/*当前任务耗时*/
	u32	ui32_MaxTaskTimeConsuming;		/*最大任务耗时*/
	void	(*pf_TimerScheduleTask)(void);	/*任务地址*/
}st_TimerSchedulerType;

/********************************************************************************************************************
|   Global Variables Definition
|*******************************************************************************************************************/

bool b_TimerBaseFlag;		/*定时时基标志*/

static	st_TimerSchedulerType	st_TimerScheduler[TIMER_TASK_NAME_DEFINITION_END];


/********************************************************************************************************************
|   Static Local Variables Definition
|*******************************************************************************************************************/

/********************************************************************************************************************
|   Static Local Function Declaration
|*******************************************************************************************************************/

/********************************************************************************************************************
|   Function Source Code
|*******************************************************************************************************************/

/********************************************************************************************************************
Function Name:		BSW_SystemService_OS_NopTask
Sync/Async:			Synchronous
Reentrancy:			Non-Reentrant
Parameter[in]:		None
Parameter[inout]:	None
Parameter[out]:		None
Return Value:		None
Description:		系统服务OS，空任务
|*******************************************************************************************************************/
static	void	BSW_SystemService_OS_NopTask(void)
{
//	_NOP();
}

/********************************************************************************************************************
Function Name:		BSW_SystemService_OS_CreateTask
Sync/Async:			Synchronous
Reentrancy:			Non-Reentrant
Parameter[in]:		e_TaskName: 任务名
					ui8_RunCount: 任务运行计数
					ui32_TickOffset: 节拍偏移量
					ui32_TimerCycle: 定时周期
					pf_Task: 任务地址
Parameter[inout]:	None
Parameter[out]:		None
Return Value:		None
Description:		系统服务OS，创建任务
|*******************************************************************************************************************/
void	BSW_SystemService_OS_CreateTask(e_TaskNameType e_TaskName, u8 ui8_RunCount,
										u32 ui32_TickOffset, u32 ui32_TimerCycle, void (*pf_Task)(void))
{
	if (e_TaskName < TIMER_TASK_NAME_DEFINITION_END)
	{
		st_TimerScheduler[e_TaskName].e_TaskStatus = TIMER_TASK_ENABLE;
		st_TimerScheduler[e_TaskName].ui8_TaskRunCount = ui8_RunCount;
		if (ui32_TimerCycle >= (u32)U16_TIMER_TOP)
		{
			ui32_TimerCycle = (u32)(U16_TIMER_TOP - U16_TIMER_BASE);
		}
		else if (ui32_TimerCycle <= (u32)U16_TIMER_BASE)
		{
			ui32_TimerCycle = (u32)U16_TIMER_BASE;
		}
		else{}
		st_TimerScheduler[e_TaskName].ui32_TaskTickOffset = ui32_TickOffset;
		st_TimerScheduler[e_TaskName].ui32_TaskTickTop = ui32_TimerCycle / (u32)U16_TIMER_BASE;
		st_TimerScheduler[e_TaskName].ui32_TaskTickCount= 0U;
		st_TimerScheduler[e_TaskName].pf_TimerScheduleTask = pf_Task;
	}
}

/********************************************************************************************************************
Function Name:		BSW_SystemService_OS_DeleteTask
Sync/Async:			Synchronous
Reentrancy:			Non-Reentrant
Parameter[in]:		e_TaskName: 任务名
Parameter[inout]:	None
Parameter[out]:		None
Return Value:		None
Description:		系统服务OS，删除任务
|*******************************************************************************************************************/
void	BSW_SystemService_OS_DeleteTask(e_TaskNameType e_TaskName)
{
	if (e_TaskName < TIMER_TASK_NAME_DEFINITION_END)
	{
		st_TimerScheduler[e_TaskName].e_TaskStatus = TIMER_TASK_IDLE;
		st_TimerScheduler[e_TaskName].ui8_TaskRunCount = 0U;
		st_TimerScheduler[e_TaskName].ui32_TaskTickTop = 0U;
		st_TimerScheduler[e_TaskName].ui32_TaskTickCount= 0U;
		st_TimerScheduler[e_TaskName].pf_TimerScheduleTask = &BSW_SystemService_OS_NopTask;
	}
	else{}
}

/********************************************************************************************************************
Function Name:		BSW_SystemService_OS_ActivateTask
Sync/Async:			Synchronous
Reentrancy:			Non-Reentrant
Parameter[in]:		e_TaskName: 任务名
					ui8_RunCount: 任务运行计数
					ui32_TimerCycle: 定时周期
Parameter[inout]:	None
Parameter[out]:		None
Return Value:		None
Description:		系统服务OS，激活任务
|*******************************************************************************************************************/
void	BSW_SystemService_OS_ActivateTask(e_TaskNameType e_TaskName, u8 ui8_RunCount, u32 ui32_TimerCycle)
{
	if (e_TaskName < TIMER_TASK_NAME_DEFINITION_END)
	{
		st_TimerScheduler[e_TaskName].e_TaskStatus = TIMER_TASK_ENABLE;
		st_TimerScheduler[e_TaskName].ui8_TaskRunCount = ui8_RunCount;
		if (ui32_TimerCycle >= (u32)U16_TIMER_TOP)
		{
			ui32_TimerCycle = (u32)(U16_TIMER_TOP - U16_TIMER_BASE);
		}
		else if (ui32_TimerCycle <= (u32)U16_TIMER_BASE)
		{
			ui32_TimerCycle = (u32)U16_TIMER_BASE;
		}
		else{}
		st_TimerScheduler[e_TaskName].ui32_TaskTickTop = ui32_TimerCycle / (u32)U16_TIMER_BASE;
		st_TimerScheduler[e_TaskName].ui32_TaskTickCount= 0U;
	}
	else{}
}

/********************************************************************************************************************
Function Name:		BSW_SystemService_OS_PauseTask
Sync/Async:			Synchronous
Reentrancy:			Non-Reentrant
Parameter[in]:		e_TaskName: 任务名
Parameter[inout]:	None
Parameter[out]:		None
Return Value:		None
Description:		系统服务OS，暂停任务
|*******************************************************************************************************************/
void	BSW_SystemService_OS_PauseTask(e_TaskNameType e_TaskName)
{
	if (e_TaskName < TIMER_TASK_NAME_DEFINITION_END)
	{
		st_TimerScheduler[e_TaskName].e_TaskStatus = TIMER_TASK_DISABLE;
		st_TimerScheduler[e_TaskName].ui32_TaskTickCount= 0U;
	}
	else{}
}

/********************************************************************************************************************
Function Name:		BSW_SystemService_OS_RunScheduler
Sync/Async:			Synchronous
Reentrancy:			Non-Reentrant
Parameter[in]:		None
Parameter[inout]:	None
Parameter[out]:		None
Return Value:		None
Description:		系统服务OS，运行调度表（定时中断中调用）
|*******************************************************************************************************************/
void	BSW_SystemService_OS_RunScheduler(void)
{
	u8	ui8_Index = 0U;

	for (ui8_Index = 0U; ui8_Index < TIMER_TASK_NAME_DEFINITION_END; ui8_Index++)
	{
		if (st_TimerScheduler[ui8_Index].e_TaskStatus == TIMER_TASK_ENABLE)
		{
			st_TimerScheduler[ui8_Index].ui32_TaskTickCount++;
		}
		else{}
	}
}

/********************************************************************************************************************
Function Name:		BSW_SystemService_OS_RunTimerTask
Sync/Async:			Synchronous
Reentrancy:			Non-Reentrant
Parameter[in]:		None
Parameter[inout]:	None
Parameter[out]:		None
Return Value:		None
Description:		系统服务OS，运行定时任务
|*******************************************************************************************************************/
void	BSW_SystemService_OS_RunTimerTask(void)
{
	u8	ui8_Index = 0U;
	u32	ui32_TimeConsuming = 0U;

	for (ui8_Index = 0U; ui8_Index < TIMER_TASK_NAME_DEFINITION_END; ui8_Index++)
	{
		if (st_TimerScheduler[ui8_Index].e_TaskStatus == TIMER_TASK_ENABLE)
		{
			if ((st_TimerScheduler[ui8_Index].ui32_TaskTickCount
				>= (st_TimerScheduler[ui8_Index].ui32_TaskTickTop + st_TimerScheduler[ui8_Index].ui32_TaskTickOffset))
				&& ((st_TimerScheduler[ui8_Index].ui8_TaskRunCount != 0U)))
			{
				st_TimerScheduler[ui8_Index].ui32_TaskTickCount = st_TimerScheduler[ui8_Index].ui32_TaskTickOffset;
				if (st_TimerScheduler[ui8_Index].ui8_TaskRunCount != U8_ALWAYS_RUN)
				{
					st_TimerScheduler[ui8_Index].ui8_TaskRunCount--;
					if (st_TimerScheduler[ui8_Index].ui8_TaskRunCount == 0U)
					{
						st_TimerScheduler[ui8_Index].e_TaskStatus = TIMER_TASK_DISABLE;
					}
					else{}
				}
				else{}
#if	TASK_TIME_CONSUMIN > 0
				BSW_SystemService_OS_StartTimeConsumingMeasurementCallout();
				(st_TimerScheduler[ui8_Index].pf_TimerScheduleTask)();
				BSW_SystemService_OS_StopTimeConsumingMeasurementCallout(&ui32_TimeConsuming);
#else
				(st_TimerScheduler[ui8_Index].pf_TimerScheduleTask)();
#endif
				if (ui32_TimeConsuming >= st_TimerScheduler[ui8_Index].ui32_MaxTaskTimeConsuming)
				{
					st_TimerScheduler[ui8_Index].ui32_MaxTaskTimeConsuming = ui32_TimeConsuming;
				}
				else{}
				st_TimerScheduler[ui8_Index].ui32_CurrentTaskTimeConsuming = ui32_TimeConsuming;
			}
			else{}
		}
		else{}
	}
}

/********************************************************************************************************************
Function Name:		BSW_SystemService_OS_StartOS
Sync/Async:			Synchronous
Reentrancy:			Non-Reentrant
Parameter[in]:		None
Parameter[inout]:	None
Parameter[out]:		None
Return Value:		None
Description:		系统服务OS，启动OS
|*******************************************************************************************************************/
void	BSW_SystemService_OS_StartOS(void)
{
	u8 u8_TaskIndex = 0U;

	b_TimerBaseFlag = FALSE;

	for(u8_TaskIndex=0;u8_TaskIndex<TIMER_TASK_NAME_DEFINITION_END;u8_TaskIndex++)
	{
		BSW_SystemService_OS_CreateTask(st_TimerSchedulerCfg[u8_TaskIndex].e_TaskName,st_TimerSchedulerCfg[u8_TaskIndex].ui8_TaskRunCount,
										st_TimerSchedulerCfg[u8_TaskIndex].ui32_TaskTickOffset,st_TimerSchedulerCfg[u8_TaskIndex].ui8_TaskTickExpect,
										st_TimerSchedulerCfg[u8_TaskIndex].pf_TimerScheduleTask);
	}

	BSW_SystemService_OS_StartSystemTimerCallout();
}

/********************************************************************************************************************
Function Name:		BSW_SystemService_OS_ShutdownOS
Sync/Async:			Synchronous
Reentrancy:			Non-Reentrant
Parameter[in]:		None
Parameter[inout]:	None
Parameter[out]:		None
Return Value:		None
Description:		系统服务OS，关闭OS
|*******************************************************************************************************************/
void	BSW_SystemService_OS_ShutdownOS(void)
{
	BSW_SystemService_OS_StopSystemTimerCallout();
}

/********************************************************************************************************************
Function Name:		BSW_SystemService_OS_SetTimerBaseFlag
Sync/Async:			Synchronous
Reentrancy:			Non-Reentrant
Parameter[in]:		None
Parameter[inout]:	None
Parameter[out]:		None
Return Value:		None
Description:		系统服务OS，置位时基标志
|*******************************************************************************************************************/
void	BSW_SystemService_OS_SetTimerBaseFlag(void)
{
	b_TimerBaseFlag = TRUE;
}

/********************************************************************************************************************
Function Name:		BSW_SystemService_OS_ReadTaskTimeConsuming
Sync/Async:			Synchronous
Reentrancy:			Non-Reentrant
Parameter[in]:		e_TaskName: 任务名称
Parameter[inout]:	None
Parameter[out]:		pui32_TaskTimeConsuming: 读取任务耗时变量地址
Return Value:		None
Description:		系统服务OS，读入任务耗时
|*******************************************************************************************************************/
void	BSW_SystemService_OS_ReadTaskTimeConsuming(e_TaskNameType e_TaskName, u32 *pui32_TaskTimeConsuming)
{
	if (e_TaskName < TIMER_TASK_NAME_DEFINITION_END)
	{
		*pui32_TaskTimeConsuming = st_TimerScheduler[e_TaskName].ui32_MaxTaskTimeConsuming;
	}
	else{}
}

/********************************************************************************************************************
Function Name:		BSW_SystemService_OS_SafetyRefresh
Sync/Async:			Synchronous
Reentrancy:			Non-Reentrant
Parameter[in]:		None
Parameter[inout]:	None
Parameter[out]:		None
Return Value:		None
Description:		系统服务OS，安全刷新
					定时周期调用此函数以刷新OS配置
|*******************************************************************************************************************/
void	BSW_SystemService_OS_SafetyRefresh(void)
{
	u8	u8_TaskIndex = 0U;
	u32  ui32_TimerCycle = 0U;

	for(u8_TaskIndex=0;u8_TaskIndex<TIMER_TASK_NAME_DEFINITION_END;u8_TaskIndex++)
	{
		st_TimerScheduler[u8_TaskIndex].e_TaskStatus = TIMER_TASK_ENABLE;	// TASK ENABLE
		st_TimerScheduler[u8_TaskIndex].ui8_TaskRunCount = st_TimerSchedulerCfg[u8_TaskIndex].ui8_TaskRunCount;		//运行次数
		ui32_TimerCycle = st_TimerSchedulerCfg[u8_TaskIndex].ui8_TaskTickExpect;
		if (ui32_TimerCycle >= (u32)U16_TIMER_TOP)
		{
			ui32_TimerCycle = (u32)(U16_TIMER_TOP - U16_TIMER_BASE);
		}
		else if (ui32_TimerCycle <= (u32)U16_TIMER_BASE)
		{
			ui32_TimerCycle = (u32)U16_TIMER_BASE;
		}
		else{}
		st_TimerScheduler[u8_TaskIndex].ui32_TaskTickTop = ui32_TimerCycle / (u32)U16_TIMER_BASE;					//运行间隔预期
		st_TimerScheduler[u8_TaskIndex].ui32_TaskTickOffset = st_TimerSchedulerCfg[u8_TaskIndex].ui32_TaskTickOffset;	//偏移
		st_TimerScheduler[u8_TaskIndex].pf_TimerScheduleTask = st_TimerSchedulerCfg[u8_TaskIndex].pf_TimerScheduleTask;;//任务地址
	}
}
